package com.wnxy.admin.task;

import com.wnxy.admin.common.util.AliOssUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/*
*  定时删除无效图片
* */
@Component
@RequiredArgsConstructor
public class CleanImageTask {

    private final RedisTemplate<String, String> redisTemplate;
    private final AliOssUtil aliOssUtil;

    @Scheduled(cron = "0 0 2 ? * SUN")
    public void cleaner() {
        Set<String> uploadFileAll = redisTemplate.opsForSet().members("upload-file-all");
        Set<String> savePersonFileDb = redisTemplate.opsForSet().members("save-person-file-db");
        Set<String> saveTaskFileDb = redisTemplate.opsForSet().members("save-task-file-db");

        Set<String> difference = new HashSet<>(uploadFileAll);
        difference.removeAll(savePersonFileDb);
        difference.removeAll(saveTaskFileDb);

        // 遍历删除
        for (String s : difference) {
            aliOssUtil.deleteImg(s);
            redisTemplate.opsForSet().remove("upload-file-all", s);
        }
    }
}
